<?php

session_start();
// checking the session
if (!$_SESSION['username']) {
    header("location:../user/login.php");
}
if ($_GET['q'] == 'logout') {
    $page->user_logout();
    header("location:../user/login.php");
}
if ($_POST['chkDel']) {
    include '../../libraries/dbconfig.php';
  
    $chk = $_POST['chkDel'];
    $rowid = rowid;



    for ($i = 0; $i < count($chk); $i++) {
        if ($chk[$i] != "") {

            $get = mysql_query("select billno,productid,rate,qty,discount,vat,total from billdetails where rowid = '$chk[$i]'");
            $get_data = mysql_fetch_array($get);
            $productid = $get_data['productid'];
            $billno = $get_data['billno'];
            $rate = $get_data['rate'];
            $qty = $get_data['qty'];
            $discount = $get_data['discount'];
            $vat = $get_data['vat'];
            $amount = $get_data['total'];
            $totalrate = ($rate - $discount) * $qty;

            $del = mysql_query("DELETE FROM billdetails WHERE rowid = '$chk[$i]'");
            if ($del) {
                $bills_update = mysql_query("Update bills set discount = discount - '$discount', vat = vat - '$vat',amount = amount - '$amount', total = total - '$totalrate'  where billno = '$billno'") or die(mysql_error());
            $invoice_update=mysql_query("Update invoicedetails set sold = sold - '$qty'  where productid = '$productid'") or die(mysql_error());
     echo '<script  type="text/javascript">
         window.alert("Deletion process is successful");
             </script>';
            
            //header("location:sales_billing_update.php?st=1");
                }
                else {
                 header("location:sales_billing_update.php?st=0");  
                }
        }
    }
}
